const mqtt = require('aliyun-iot-mqtt');
// 1. 设备身份信息
var options = {
    productKey: "k16p49hwseJ",
    deviceName: "Smoke_alarm_66",
    deviceSecret: "49c1161049c7447aa39d8d9865c7c401",
    host: "iot-06z00jkc5oqsydm.mqtt.iothub.aliyuncs.com"
};

// 2. 建立MQTT连接
const client = mqtt.getAliyunIotMqttClient(options);
//订阅云端指令Topic
client.subscribe(`/${options.productKey}/${options.deviceName}/user/get`)
client.subscribe(`/sys/${options.productKey}/${options.deviceName}/thing/event/property/post_reply`)
client.on('message', function (topic, message) {
    console.log("topic " + topic)
    console.log("message " + message)
})

setInterval(function () {
    // 3.定时上报温湿度数据
    client.publish(`/sys/${options.productKey}/${options.deviceName}/thing/event/property/post`, getPostData(), { qos: 0 });
}, 5 * 1000);

var power = 100;
var data = 20;

function getPostData () {
    const payloadJson = {
        id: Date.now(),
        version: "1.0",
        params: {
            CurrentHumidity: power--,
            BatteryLevel: power--,
            SmokeSensorState: power--,
            IndoorTemperature: data++,
            // PowerConsumption: power--
        },
        method: "thing.event.property.post"

    }
    console.log("payloadJson " + JSON.stringify(payloadJson))
    return JSON.stringify(payloadJson);
}